package com.carey.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carey.yygh.common.exception.YyghException;
import com.carey.yygh.common.result.ResultCodeEnum;
import com.carey.yygh.common.util.MD5;
import com.carey.yygh.hosp.service.HospitalSetService;
import com.carey.yygh.model.hosp.HospitalSet;
import com.carey.yygh.vo.hosp.HospitalSetQueryVo;
import com.carey.yygh.vo.order.SignInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.carey.yygh.common.result.Result;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
//@CrossOrigin  //允许跨域访问
@RequestMapping("admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
     public Result findall(){
         List<HospitalSet> list = hospitalSetService.list();
         return Result.ok(list);
     }

    @ApiOperation(value = "逻辑删除医院设置")
     @DeleteMapping("{id}")
    public Result deletehosp(@PathVariable Long id){
        boolean resoult = hospitalSetService.removeById(id);
        if (resoult){
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
     }

     //--条件分页查询

    @ApiOperation(value = "按条件分页查询")
    @PostMapping("querypagehosp/{current}/{limit}")
    public Result queryPageHosp(@PathVariable("current") Integer current ,
                                @PathVariable("limit") Integer limit,
                                //方便前端传数据 json查询，required = false意思是值可以为空
                                @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo)
    {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        //判断查询条件是否为空
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        //mp的分页查询
        Page<HospitalSet> page = new Page<>(current,limit);
        hospitalSetService.page(page, queryWrapper);
        /*List<HospitalSet> list = page.getRecords();
        list.forEach(System.out::println);
        System.out.println("当前页："+page.getCurrent());
        System.out.println("每页显示的条数："+page.getSize());
        System.out.println("总记录数："+page.getTotal());
        System.out.println("总页数："+page.getPages());
        System.out.println("是否有上一页："+page.hasPrevious());
        System.out.println("是否有下一页："+page.hasNext());*/
        return Result.ok(page);
    }

    //--添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("addhosptailset")//@RequestBody要和postmapping一起用
    public Result addHopstailSet(@RequestBody(required = false)HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        //设置签名秘钥
        Random random = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(key);
        //添加医院设置
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok("添加成功");
        }else {
            return Result.fail("添加失败，请确认后重新添加");
        }
    }

    //--根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("queryhosptailsetbyid/{id}")
    public Result queryHosptailSetByid(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //--修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("updatehosptailset")
    public Result updateHosptailSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok("修改成功");
        }else {
            return Result.fail("修改失败，请稍后重试");
        }
    }

    //--批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchhosptailset")
    public Result deleteHosptailSet(@RequestBody List<String> idlist){
        boolean flag = hospitalSetService.removeByIds(idlist);
        if (flag){
            return Result.ok("删除成功");
        }else {
            return Result.fail("删除失败，请稍后重试");
        }
    }

    //医院设置锁定或解锁
    @ApiOperation("医院设置锁定或解锁")
    @PutMapping("lockhosptailset/{id}/{status}")
    public Result lockHosptailSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //发送签名密钥
    @ApiOperation("发送签名密钥")
    @PutMapping("sendsinkey/{id}")
    public Result sendsinkey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        //TODO 发送短信/发送邮件
        return Result.ok();
    }

    /**
     * 10.模拟异常处理
     */
    @ApiOperation("模拟异常")
    @GetMapping("/mock")
    public Result exceptionHandle(){
        // int i = 1/0;
        try {
            int i = 1/0;
        }catch (Exception e) {
            throw new YyghException("出现异常了",204);
        }
        return Result.ok();
    }

    @ApiOperation(value = "获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") String hoscode) {
        return hospitalSetService.getSignInfoVo(hoscode);
    }


}
